// pages/history/history.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    checkedAll: false,
    isDel: false,
    result: [],
    content: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.request({
      url: `http://localhost:1234/history`,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'history': JSON.stringify(wx.getStorageSync('history'))
      },
      method: 'POST',
      success: function (res) {
        let status = res.data.status;
        if (status == 200) {
          that.setData({
            content: res.data.content,
          })
        }
      },
      fail: function () {
        console.log("加载失败")
      }
    })
  },

  arrange: function () {
    this.setData({
      isDel: !this.data.isDel
    })
    if (this.data.isDel == false) {
      this.setData({
        result: [],
        checkedAll: false
      })
    }
  },

  onChange(event) {
    this.setData({
      result: event.detail
    });
    console.log(this.data.result);
    if (this.data.result.length !== this.data.content.length) {
      this.setData({
        checkedAll: false
      })
    }
    else {
      this.setData({
        checkedAll: true
      })
    }

  },
  allChecked(event) {
    /* 全选功能 */
    this.setData({
      checkedAll: !this.data.checkedAll
    })

    if (this.data.checkedAll == true) {
      let list = this.data.content.map(item => {
        return JSON.stringify(item.id)
      })
      this.setData({
        result: list
      })
    }
    else {
      this.setData({
        result: []
      })
    }
  },

  boxDel: function () {
    wx.showModal({
      title: '确定删除？',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        console.log(this.data.result)
        if (res.confirm) {
          let list = this.data.content.filter(item => !this.data.result.some((res) => item.id == res))
          let arr = wx.getStorageSync('history')
          let history = arr.filter(item => !this.data.result.some((res) => item == res))
          console.log(history)
          wx.setStorageSync('history', history)
          this.setData({
            content: list,
            result: []
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})